/**
 * Created by Administrator on 15-7-1.
 */
var XHR = {
	/**
	 * 从服务端获取服务列表
	 * @param hash
	 * @param type
	 *   ** EVENT_SUBSCRIBE 关注时回复 欢迎信息
	 *   ** DEFAULT         默认默认回复 指定系统不知道该如何回复粉丝的消息时，发送的默认消息
	 * @param config
	 */
	getReply : function(hash,type,config){
		var fn = new Function;
		var config = config || config;
		if(!hash){
			console && console.log("缺省hash");
			return false;
		}
		if(!type){
			console && console.log("缺省type");
			return false;
		}
		type = type.toUpperCase();
		var loading = config.loading || fn;
		var removeLoading = config.removeLoading || fn;
		var success = config.success || fn;
		var fail = config.fail || fn;
		var timeout = config.timeout || fn;
		var error = config.error || fn;
		$.ajax({
			url : WXB.URLS.api,
			type : "GET",
			dataType : "JSON",
			timeout : WXB.AJAX_TIMEOUT,
			data : {
				"action" : "GetSpecialResp",
				hash : hash,
				key : type
			},
			beforeSend : function(){ loading()},
			success : function(res){
				removeLoading();
				if(res && res.status == "ok"){
					success(res);
				}else if( res && res.status == "fail"){
					fail(res);
				}else{
					error();
				}
			},
			error : function(xhr,txt){
				removeLoading();
				if(txt == "timeout"){
					timeout();
				}else{
					error();
				}
			}
		})
	},
	submit : {
		/**
		 * 提交保存--默认回复
		 * @param hash
		 * @param response 默认回复的内容
		 * @param config
		 */
		"DEFAULT" : function(hash,response,config){
			var fn = new Function;
			var config = config || {};
			var loading = config.loading || fn;
			var removeLoading = config.removeLoading || fn;
			var success = config.success || fn;
			var fail = config.fail || fn;
			var timeout = config.timeout || fn;
			var error = config.error || fn;
			var serverError = config.serverError || fn;
			if(!hash){
				error("缺省hash");
				return false;
			}
			$.ajax({
				url : WXB.URLS.api,
				type : "POST",
				dataType : "JSON",
				data : {"action":"SetKeyWord","hash":hash,"keyword":"DEFAULT","response":response},
				timeout : WXB.AJAX_TIMEOUT,
				beforeSend : function(){loading();},
				success : function(res){
					removeLoading();
					if(res && res.status == "ok"){
						success(res);
					}else if(res && res.status == "fail"){
						fail(res);
					}else{
						serverError();
					}
				},
				error : function(xhr,txt){
					removeLoading();
					if(txt == "timeout"){
						timeout();
					}else{
						serverError();
					}
				}
			})
		},
		/**
		 * 提交保存--欢迎消息--纯文字
		 * @param hash
		 * @param response
		 * @param config
		 */
		"EVENT_SUBSCRIBE_TEXT" : function(hash,response,config){
			response = response || "";
			var fn = new Function;
			var config = config || {};
			var loading = config.loading || fn;
			var removeLoading = config.removeLoading || fn;
			var success = config.success || fn;
			var fail = config.fail || fn;
			var error = config.error || fn;
			var timeout = config.timeout || fn;
			var serverError = config.serverError || fn;
			if(!hash){
				error("缺省hash");
				return false;
			}
			$.ajax({
				url : WXB.URLS.api,
				type : "POST",
				dataType : "JSON",
				data : {"action":"SetKeyWord","hash":hash,"keyword":"EVENT_SUBSCRIBE","response":response},
				timeout : WXB.AJAX_TIMEOUT,
				beforeSend : function(){loading();},
				success : function(res){
					removeLoading();
					if(res && res.status == "ok"){
						success(res);
					}else if(res && res.status == "fail"){
						fail(res);
					}else{
						serverError();
					}
				},
				error : function(xhr,txt){
					removeLoading();
					if(txt == "timeout"){
						timeout();
					}else{
						serverError();
					}
				}
			})
		},
		/**
		 * 提交保存--欢迎消息--图文
		 * @param hash
		 * @param data
		 * @param config
		 */
		"EVENT_SUBSCRIBE_IMG_TEXT" : function(hash,data,config){
			var fn = new Function;
			var config = config || {};
			var loading = config.loading || fn;
			var removeLoading = config.removeLoading || fn;
			var success = config.success || fn;
			var fail = config.fail || fn;
			var timeout = config.timeout || fn;
			var error = config.error || fn; //所传参数不符合要求时
			var serverError = config.serverError || fn;
			if(!hash){
				error("缺省hash");
				return false;
			}
			$.ajax({
				url : WXB.URLS.api,
				type : "POST",
				dataType : "JSON",
				data : {
					"hash" : hash,
					"keyword" : "EVENT_SUBSCRIBE",
					"msg_type" : "IMAGE",
					"action":"SetKeyWord",
					"lists" : data
				},
				timeout : WXB.AJAX_TIMEOUT,
				beforeSend : function(){loading();},
				success : function(res){
					removeLoading();
					if(res && res.status == "ok"){
						success(res);
					}else if(res && res.status == "fail"){
						fail(res);
					}else{
						serverError();
					}
				},
				error : function(xhr,txt){
					removeLoading();
					if(txt == "timeout"){
						timeout();
					}else{
						serverError();
					}
				}
			})
		}
	}
};